package com.hmdp.utils;

import com.hmdp.mapper.ShopMapper;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

import static com.hmdp.utils.RedisConstants.EXIST_KEY;

/**
 * 缓存重建
 *
 * @author wanghaidong
 * @date 2023/08/09
 */
@Component
public class CacheRebuild {
    private BloomFilter bloomFilter;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private ShopMapper shopMapper;

    public CacheRebuild() {
    }

    @PostConstruct
    private void initDepend(){
        String key = EXIST_KEY + "shop";
        this.bloomFilter = new BloomFilter(stringRedisTemplate, key, 10000);
        // 启动时重建位图缓存
        List<Long> ids = shopMapper.getAllIds();
        for (Long id : ids){
            bloomFilter.add("" + id);
        }
    }
}
